/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.snda.lidejia.jobflow.task;

import com.snda.lidejia.jobflow.exception.JobFlowException;

/**
 *
 * @author lidejia
 */
public class SingleThreadTask extends AbstractTask {

    private Class<? extends TaskHandler> taskHandlerClass = null;

    public SingleThreadTask(Class<? extends TaskHandler> taskHandlerClass) {
        this.taskHandlerClass = taskHandlerClass;
    }

    public TaskData run(TaskData inputs) throws JobFlowException {
        try {
            TaskHandler handler = taskHandlerClass.newInstance();
            handler.setTask(this);
            // let the handler to handle the task
            TaskData outputs = handler.handler(inputs);
            // if handler return null, then we just return a empty TaskData object
            if (outputs == null) {
                outputs = new TaskData();
            }
            return outputs;
        } catch (Exception ex) {
            throw new JobFlowException(ex);
        }
    }
}
